﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace XMLPerformace
{
    public partial class Form1 : Form
    {
        const string pathFileTool=@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe";
        public Form1()
        {
            InitializeComponent();
        }
        private string fullPathFileConfig = null;
        private string fullPathFileTool = pathFileTool;

        private void btnChoseeFile_Click(object sender, EventArgs e)
        {
            DialogResult tan = openFileDialog1.ShowDialog();
            if (tan == DialogResult.Cancel)
            {
                return;
            }
            string filePath = openFileDialog1.FileName;

            FileInfo file = new FileInfo(filePath);
            fullPathFileConfig = file.FullName;
            
            rtBxml.Text = EncryptHelper.GetStringValueFromXML(fullPathFileConfig);

        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            try
            {
                EnDecrypt(true);
               
            }
            catch
            {
                MessageBox.Show("Check file input!");

                return;
            }
            rtBxml.Text = EncryptHelper.GetStringValueFromXML(fullPathFileConfig);
            
        }

        private void btnDeEncrypt_Click(object sender, EventArgs e)
        {
            try
            {
                
                EnDecrypt(false);
            }
            catch
            {
                MessageBox.Show("Check file input or Directory parrent must have Default.aspx !");
             
                return;
            }
            rtBxml.Text = EncryptHelper.GetStringValueFromXML(fullPathFileConfig);
        }
        private void EnDecrypt(bool isEncrypt)
        {
            List<string> listSection = new List<string>();
            if (cbAppSettings.Checked == true)
                listSection.Add("appSettings");
            if (cbConnectionString.Checked == true)
                listSection.Add("connectionStrings");
            if (listSection.Count == 0)
            {
                string mess;
                if (isEncrypt == true)
                    mess = "Please section need to encrypt!";
                else
                    mess = "Please section need to dencrypt!";

                MessageBox.Show(mess);
                return;
            }
            EncryptHelper.Ecrypt(fullPathFileTool, fullPathFileConfig, listSection, isEncrypt);
        }
    }
}
